package com.situ.water_pay.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.util.ResponseData;
import com.situ.water_Invoice.pojo.IvInvoice;
import com.situ.water_Invoice.service.IvInvoiceService;
import com.situ.water_pay.pojo.PyPay;
import com.situ.water_pay.service.PyPayService;
import com.situ.water_system.mapper.SyEmpMapper;
import com.situ.water_system.pojo.SyEmp;
import com.situ.water_system.service.SyEmpService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Angel
 * @since 2020-12-01
 */
@RestController
@RequestMapping("/py-pay")
public class PyPayController {
    @Autowired
    private PyPayService pyPayService;
    @Autowired
    private IvInvoiceService ivInvoiceService;
    @Autowired
    private SyEmpService syEmpService;

    /**
     * 条件查询交费单
     *
     * @return
     */
    @GetMapping("/findAll")
    public ResponseData findAll(@RequestParam(defaultValue = "0", required = false) Integer EmpID, Integer invoice, String PayDate) {
        try {
            QueryWrapper wrapper = new QueryWrapper();
            if (EmpID != 0) {
                wrapper.eq("EmpID", EmpID);
            }
            if (invoice != null) {
                wrapper.like("Invoice", invoice);
            }
            if (PayDate != null && PayDate != "") {
                wrapper.likeRight("PayDate", PayDate);
            }
            List<PyPay> PyPayList = pyPayService.list(wrapper);
            for (PyPay pyPay : PyPayList) {
                try {
                    IvInvoice invoice1 = ivInvoiceService.getById(pyPay.getInvoice());
                    pyPay.setIvInvoice(invoice1);
                } catch (Exception e) {
                    IvInvoice invoice1 = ivInvoiceService.getById(pyPay.getInvoice());
                    pyPay.setIvInvoice(invoice1);
                }
            }
            return ResponseData.ok().putDataValue("PyPayList", PyPayList);
        } catch (
                Exception e) {
            e.printStackTrace();
            return ResponseData.Failure("查询交费单失败");
        }

    }

    /**
     * 根据用户编号查询交费单
     *
     * @param UserNo
     * @return
     */
    @GetMapping("/findByUserNo")
    public ResponseData findByUserNo(String UserNo) {
        try {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("UserNo", UserNo);
//            wrapper.orderByDesc("PayDate");
            List<PyPay> PyPayList = pyPayService.list(wrapper);
            return ResponseData.ok().putDataValue("PyPayList", PyPayList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.Failure("查询交费单失败");
        }
    }

    /**
     * 查询收费部所有员工
     *
     * @return
     */
    @GetMapping("/findAllEmp")
    public ResponseData findAllEmp() {
        try {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("DeptID", 5);
            List<SyEmp> empList = syEmpService.list(wrapper);
            return ResponseData.ok().putDataValue("empList", empList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.Failure("查询员工失败");
        }
    }

    /**
     * 根据交费单号查询交费单信息
     *
     * @param payNo
     * @return
     */
    @GetMapping("/findByPayNo")
    public ResponseData findByPayNo(String payNo) {
        try {
            PyPay pyPay = pyPayService.getById(payNo);
            return ResponseData.ok().putDataValue("pyPay", pyPay);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.Failure("根据编号查询交费单失败");
        }
    }

    /**
     * 修改水费单的发票号
     *
     * @param payNo
     * @param InvoiceNo1 修改前的发票号
     * @param InvoiceNo2 修改后的发票号
     * @return
     */
    @PostMapping("/updateInvoiceNo")
    public ResponseData updateInvoiceNo(String payNo, String InvoiceNo1, String InvoiceNo2) {
        try {
            //查询交费单
            PyPay pyPay = pyPayService.getById(payNo);
            //查询新发票
            IvInvoice newInvoice = ivInvoiceService.getById(InvoiceNo2);
            if (newInvoice != null) {
                if (newInvoice.getUsed()) {
                    return ResponseData.Failure("发票号码" + InvoiceNo2 + "已被使用");
                } else {
                    //设置新发票号已使用
                    newInvoice.setUsed(true);
                    newInvoice.setUseDate(new Date());
                    newInvoice.setEmpID(pyPay.getEmpID());
                    ivInvoiceService.updateById(newInvoice);
                    //设置老发票号为作废
                    IvInvoice oldInvoices = ivInvoiceService.getById(InvoiceNo1);
                    oldInvoices.setInvalid(true);
                    oldInvoices.setInvalidDate(new Date());
                    ivInvoiceService.updateById(oldInvoices);
                    //更新交费单发票号码
                    pyPay.setInvoice(newInvoice.getInvoiceNo());
                    pyPayService.updateById(pyPay);
                    return ResponseData.ok();
                }
            } else {
                return ResponseData.Failure("发票号码" + InvoiceNo2 + "不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.Failure("更新发票号码失败");
        }
    }
/******************************收费情况报表********************************/
    /***
     *分页条件查询交费信息
     * @param userNo 用户编号
     * @param empId 员工编号
     * @param minDate 起始时间
     * @param maxDate 截止时间
     * @param minInvoice 起始发票号
     * @param maxInvoice 截止发票号
     * @param pageSize
     * @return
     */
    @PostMapping("/pageQuery")
    public ResponseData pageQuery(String userNo, Integer empId, String minDate, String maxDate, String minInvoice, String maxInvoice,
                                  @RequestParam(defaultValue = "1", required = false) Integer page, @RequestParam(defaultValue = "5", required = false) Integer pageSize) {
        try {
            QueryWrapper<PyPay> wrapper = new QueryWrapper();
            if (userNo != null && userNo != "") {
                wrapper.eq("UserNo",userNo);
            }
            if(empId!=null) {
                Integer id = Integer.valueOf(empId);
                if (id!=0) {
                    wrapper.eq("EmpID", empId);
                }
            }
            if(minDate!=null&&minDate!=""){
                wrapper.apply("date_format (PayDate,'%Y-%m-%d') >= date_format('" + minDate + "','%Y-%m-%d')");
            }
            if(maxDate!=null&&maxDate!=""){
                wrapper.apply("date_format (PayDate,'%Y-%m-%d') <= date_format('" + maxDate + "','%Y-%m-%d')");
            }
            if(minInvoice!=null&&minInvoice!=""){
                wrapper.apply("Invoice>"+minInvoice);
            }
            if(maxInvoice!=null&&maxInvoice!=""){
                wrapper.apply("Invoice<"+maxInvoice);
            }
            Page payPage = new Page(page,pageSize);
            IPage pypayPage = pyPayService.page(payPage, wrapper);
            return ResponseData.ok().putDataValue("pypayPage",pypayPage);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.Failure("查询收费情况失败");
        }
    }

}

